package  
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	import org.thot.model.ge.GameEvent;
	import org.thot.model.ge.GameEventPool;
	import org.thot.display.GameEventLib;
	import ScriptAPI;
	/**
	 * ...
	 * @author Thot
	 */
	public class Main extends Sprite
	{
		private var m_text:TextField;
		
		public function Main() 
		{
			var l_loader:Loader = new Loader();
			l_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler );
			l_loader.load( new URLRequest( "level1.swf" ) );
		}
		
		private function completeHandler(e:Event):void 
		{
			
			
			m_text = addChild( new TextField() ) as TextField;
			m_text.width = m_text.height = 600;
			m_text.y = 35;
			
			
			var l_loader:Loader = LoaderInfo( e.target ).loader;
			l_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler );
			
			var l_swf:GameEventLib = l_loader.content as GameEventLib;
			var l_pool:GameEventPool = new GameEventPool();
			var l_ge:GameEvent;
			
			ScriptAPI.setGlobalVar( "root", this );
			
			
			for each( l_ge in l_swf.gameEvents )
			{
				l_ge.scriptAPI = ScriptAPI;
				l_pool.addGameEvent( l_ge );
			}
			
			var l_timer:int = getTimer();
			
			var i:int = 0;
			var l_max:int = 100000;			
			
			for ( ; i < l_max; i++ )
			{
				l_pool.launchTrigger( "initApp" );
				l_pool.launchTrigger( "start_DisplayMessage_Cyclic" );
			}
		
			l_pool.launchTrigger( "start_DisplayMessage_Cyclic" );
			
			
			var l_str:String = "total time: " + String( getTimer() - l_timer ) + ", " + String( ( getTimer() - l_timer ) / l_max ) + " ms per event";
			
			addText( l_str );
			
		}
		
		public function addText( p_text:String ):void
		{
			m_text.appendText( "\n" + p_text );
		}
		
	}

}